package com.schoolvisitors.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.CheckPermission;
import com.schoolvisitors.entity.Floor;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.exception.BusinessException;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.response.ResultCode;
import com.schoolvisitors.service.FloorService;
import jakarta.validation.Valid;
import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/admin")
public class AdminFloorController {

    @Autowired
    private FloorService floorService;

    // 创建楼层
    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/createFloor")
    public ResultBody createFloor(@Valid @RequestBody Floor floor) {
        Floor createdFloor = floorService.createFloor(floor);
        return ResultBody.success(createdFloor);
    }

    // 删除楼层
    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/deleteFloor")
    public ResultBody deleteFloor(@RequestBody JSONObject json) {
        Long floorId = json.getLong("floorId");
        if (floorId == null) {
            throw new BusinessException(ResultCode.FLOOR_ID_NOT_FOUND);
        }
        floorService.deleteFloor(floorId);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getFloors")
    public ResultBody getFloors() {
        List<Floor> floorList = floorService.getFloors();
        return ResultBody.success(floorList);
    }
}
